package com.example.cheeseerpadmin.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.BusCustomer;
import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusSales;
import com.example.cheeseerpadmin.bus.service.BusCustomerService;
import com.example.cheeseerpadmin.bus.service.BusGoodsService;
import com.example.cheeseerpadmin.bus.service.BusSalesService;
import com.example.cheeseerpadmin.bus.vo.SalesVo;
import com.example.cheeseerpadmin.sys.common.Constant;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import com.example.cheeseerpadmin.sys.common.WebUtils;
import com.example.cheeseerpadmin.sys.entity.SysUser;
import com.sun.org.apache.regexp.internal.RE;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    public BusSalesService busSalesService;
    @Autowired
    public BusGoodsService busGoodsService;
    @Autowired
    public BusCustomerService busCustomerService;


    /**
     * 加载全部的销售进货数据
     *
     * @param salesVo
     * @return
     */
    @RequestMapping("/loadAllSales")
    public DataGridView loadAllSales(SalesVo salesVo) {
        Page<BusSales> busSalesPage = new Page<>();
        QueryWrapper<BusSales> busSalesQueryWrapper = new QueryWrapper<>();
        busSalesQueryWrapper.eq(salesVo.getCustomerid() != null && salesVo.getCustomerid() != 0, "customerid", salesVo.getCustomerid());
        busSalesQueryWrapper.eq(salesVo.getGoodsid() != null && salesVo.getGoodsid() != 0, "goodsid", salesVo.getGoodsid());
        busSalesQueryWrapper.like(StringUtils.isNotBlank(salesVo.getOperateperson()), "operateperson", salesVo.getOperateperson());
        busSalesQueryWrapper.like(StringUtils.isNotBlank(salesVo.getRemark()), "remark", salesVo.getRemark());
        busSalesQueryWrapper.ge(salesVo.getStartDate() != null, "salestime", salesVo.getStartDate());
        busSalesQueryWrapper.le(salesVo.getEndDate() != null, "salestime", salesVo.getEndDate());
        busSalesQueryWrapper.orderByDesc("salestime");
        Page<BusSales> page = busSalesService.page(busSalesPage, busSalesQueryWrapper);
        List<BusSales> records = page.getRecords();
        for (BusSales busSales : records) {
            Integer customerid = busSales.getCustomerid();
            Integer goodsid = busSales.getGoodsid();
            BusCustomer busCustomer = busCustomerService.getById(customerid);
            if (busCustomer != null) {
                String customername = busCustomer.getCustomername();
                busSales.setCustomername(customername);
            }
            BusGoods busGoods = busGoodsService.getById(goodsid);
            if (busGoods != null) {
                String goodsname = busGoods.getGoodsname();
                String size = busGoods.getSize();
                busSales.setGoodsname(goodsname);
                busSales.setSize(size);
            }
        }
        return new DataGridView((long) records.size(), records);
    }

    /**
     * 增加商品销售信息
     *
     * @param busSales
     * @return
     */
    @RequestMapping("/addSales")
    public ResultObj addSales(BusSales busSales) {
        try {
            busSales.setSalestime(new Date());
            SysUser user = (SysUser) WebUtils.getSession().getAttribute("user");
            busSales.setOperateperson(user.getName());
            busSales.setBalance(busSales.getNumber());
            busSalesService.save(busSales);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_SUCCESS;
        }
    }

    /**
     * 更新商品销售信息
     *
     * @param busSales
     * @return
     */
    @RequestMapping("/updateSales")
    public ResultObj updateSales(BusSales busSales) {
        try {
            busSalesService.updateById(busSales);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询所有可用客户供销售管理选择
     *
     * @return
     */
    @RequestMapping("/loadAllCustomerForSelect")
    public DataGridView loadAllCustomerForSelect() {
        QueryWrapper<BusCustomer> busCustomerQueryWrapper = new QueryWrapper<>();
        busCustomerQueryWrapper.eq("available", Constant.AVAILABLE_TRUE);
        List<BusCustomer> list = busCustomerService.list(busCustomerQueryWrapper);
        return new DataGridView((long) list.size(), list);
    }

    /**
     * 查询所有可用商品供销售管理选择
     *
     * @return
     */
    @RequestMapping("/loadGoodsByCustomerId")
    public DataGridView loadGoodsByCustomerId() {
        QueryWrapper<BusGoods> busGoodsQueryWrapper = new QueryWrapper<>();
        busGoodsQueryWrapper.eq("available", Constant.AVAILABLE_TRUE);
        List<BusGoods> list = busGoodsService.list(busGoodsQueryWrapper);
        return new DataGridView((long) list.size(), list);
    }

    /**
     * 删除商品销售信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteSalesById")
    public ResultObj deleteSalesById(Integer id) {
        try {
            busSalesService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }

    }
}

